package onepanel

import (
	"context"
	"fmt"
	"net/http"
)

type WebsiteGetRequest struct {
	Name     string `json:"name"`
	Type     string `json:"type"`
	Page     int32  `json:"page"`
	PageSize int32  `json:"pageSize"`
}

type WebsiteGetResponse struct {
	apiResponseBase

	Data *struct {
		ID            int64  `json:"id"`
		Alias         string `json:"alias"`
		PrimaryDomain string `json:"primaryDomain"`
		Protocol      string `json:"protocol"`
		Type          string `json:"type"`
		Status        string `json:"status"`
		SitePath      string `json:"sitePath"`
		Remark        string `json:"remark"`
		Domains       []*struct {
			ID        int64  `json:"id"`
			Domain    string `json:"domain"`
			Port      int32  `json:"port"`
			SSL       bool   `json:"ssl"`
			UpdatedAt string `json:"updatedAt"`
			CreatedAt string `json:"createdAt"`
		} `json:"domains"`
		WebsiteSSLId int64  `json:"webSiteSSLId"`
		UpdatedAt    string `json:"updatedAt"`
		CreatedAt    string `json:"createdAt"`
	} `json:"data,omitempty"`
}

func (c *Client) WebsiteGet(websiteId int64) (*WebsiteGetResponse, error) {
	return c.WebsiteGetWithContext(context.Background(), websiteId)
}

func (c *Client) WebsiteGetWithContext(ctx context.Context, websiteId int64) (*WebsiteGetResponse, error) {
	if websiteId == 0 {
		return nil, fmt.Errorf("sdkerr: unset websiteId")
	}

	httpreq, err := c.newRequest(http.MethodGet, fmt.Sprintf("/websites/%d", websiteId))
	if err != nil {
		return nil, err
	} else {
		httpreq.SetContext(ctx)
	}

	result := &WebsiteGetResponse{}
	if _, err := c.doRequestWithResult(httpreq, result); err != nil {
		return result, err
	}

	return result, nil
}
